#ifndef __usb_host_serial_h__
#define __usb_host_serial_h__
/*HEADER**********************************************************************
*
* Copyright 2008 Freescale Semiconductor, Inc.
* Copyright 1989-2008 ARC International
*
* This software is owned or controlled by Freescale Semiconductor.
* Use of this software is governed by the Freescale MQX RTOS License
* distributed with this Material.
* See the MQX_RTOS_LICENSE file distributed for more details.
*
* Brief License Summary:
* This software is provided in source form for you to use free of charge,
* but it is not open source software. You are allowed to use this software
* but you cannot redistribute it or derivative works of it in source form.
* The software may be used only in connection with a product containing
* a Freescale microprocessor, microcontroller, or digital signal processor.
* See license agreement file for full license terms including other
* restrictions.
*****************************************************************************
*
* Comments:
*
*   This file defines a template structure for Class Drivers.
*
*
*END************************************************************************/
#include <mqx.h>
#include <lwevent.h>

#include <fio.h>

#include <bsp.h>

#include "usb.h"
#include "usb_prv.h"

#include "host_cnfg.h"
#include "hostapi.h"
#include "host_rcv.h"
#include "host_ch9.h"
#include "host_snd.h"

typedef struct {
    uint8_t   bFunctionLength;
    uint8_t   bDescriptorType;
    uint8_t   bDescriptorSubtype;
    uint8_t   bcdCDC[2];
} USB_SERIAL_DESC_HEADER, * USB_SERIAL_DESC_HEADER_PTR;

#define USB_DESC_SUBTYPE_CS_MODEM               0x01
typedef struct {
    uint8_t   bFunctionLength;
    uint8_t   bDescriptorType;
    uint8_t   bDescriptorSubtype;
    uint8_t   bmCapabilities;
    uint8_t   bDataInterface;
} USB_SERIAL_DESC_MODEM, * USB_SERIAL_DESC_MODEM_PTR;

#define USB_DESC_SUBTYPE_CS_PCUI              0x02
typedef struct {
    uint8_t   bFunctionLength;
    uint8_t   bDescriptorType;
    uint8_t   bDescriptorSubtype;
    uint8_t   bmCapabilities;
} USB_SERIAL_DESC_PCUI, * USB_SERIAL_DESC_PCUI_PTR;

#define USB_DESC_SUBTYPE_CS_DIAG               0x03
typedef struct {
    uint8_t   bFunctionLength;
    uint8_t   bDescriptorType;
    uint8_t   bDescriptorSubtype;
    uint8_t   bmCapabilities;
} USB_SERIAL_DESC_DIAG, * USB_SERIAL_DESC_DIAG_PTR;

typedef union {
    USB_SERIAL_DESC_HEADER      header;
    USB_SERIAL_DESC_MODEM       modem;
    USB_SERIAL_DESC_PCUI        pcui;
    USB_SERIAL_DESC_DIAG        diag;    
} USB_SERIAL_FUNC_DESC, * USB_SERIAL_FUNC_DESC_PTR;

/*
** CDC Class Interface structure. This structure will be passed to
** all commands to this class driver.
*/
typedef struct usb_serial_general_class {
    /* Each class must start with a GENERAL_CLASS struct */
    GENERAL_CLASS                             G;
    /* Higher level callback and parameter */
    tr_callback                               USER_CALLBACK;
    void                                     *USER_PARAM;
    uint8_t                                    IFNUM;

} USB_SERIAL_GENERAL_CLASS, * USB_SERIAL_GENERAL_CLASS_PTR;

#define USB_SERIAL_DETACH            0x01
#define USB_SERIAL_READ_COMPLETE     0x02
#define USB_SERIAL_READ_PIPE_FREE    0x04
#define USB_SERIAL_SEND_COMPLETE     0x08
#define USB_SERIAL_SEND_PIPE_FREE    0x10

typedef struct {
    /* Each CDC subclass must start with a USB_CDC_GENERAL_CLASS struct */
    USB_SERIAL_GENERAL_CLASS            SERIAL_G;

    char                                *RX_BUFFER;
    char                                *INT_IN_BUFFER;
    char                                *RX_BUFFER_DRV;
    char                                *RX_BUFFER_APP;
    uint32_t                            RX_BUFFER_SIZE;
    uint32_t                            RX_READ;
    
    uint32_t                            TX_SENT;

    PIPE_STRUCT_PTR                     in_pipe;
    PIPE_STRUCT_PTR                     out_pipe;
    PIPE_STRUCT_PTR                     int_in_pipe; 

	/* Settings for the port */
	int                                 rts_state;	/* Handshaking pins (outputs) */
	int                                 dtr_state;
	int                                 cts_state;	/* Handshaking pins (inputs) */
	int                                 dsr_state;
	int                                 dcd_state;
	int                                 ri_state;
   
    char                                *device_name;

    LWEVENT_STRUCT_PTR                  serial_event;

} USB_SERIAL_CLASS_INTF_STRUCT, * USB_SERIAL_CLASS_INTF_STRUCT_PTR;

#define USB_UART_BLOCKING          (0x0000)
#define USB_UART_NO_BLOCKING       (0x0001)
/* NOTE!!!
** HW control flow has one weakness: we dont know exactly when DCD signal got log. 0 or
** log.1. This is because interrupts pipe is polled in time intervals in some milliseconds.
** HW flow control is here only just for compatibility purposes.
** As for output pins, we set always DTR and RTS signal for HW flow control. The user can
** still use usb_class_serial_set_acm_ctrl_state to control those signals manually.
** Note also, that additional HW control flow is done on USB,
** which implies from USB specification (NAK responding).
*/
#define USB_UART_HW_FLOW           (0x0002)

typedef struct usb_serial_init {
    _mqx_uint               FLAGS;
} USB_SERIAL_INIT, * USB_SERIAL_INIT_PTR;

/* Class specific functions exported by CDC class driver */
#ifdef __cplusplus
extern "C" {
#endif

extern void usb_class_serial_init(PIPE_BUNDLE_STRUCT_PTR, CLASS_CALL_STRUCT_PTR);
extern USB_STATUS usb_class_serial_use_lwevent(CLASS_CALL_STRUCT_PTR, LWEVENT_STRUCT_PTR);
extern CLASS_CALL_STRUCT_PTR usb_class_serial_get_interface(void *);
extern USB_STATUS usb_class_serial_set_interface(CLASS_CALL_STRUCT_PTR,INTERFACE_DESCRIPTOR_PTR);

#if MQX_USE_IO_OLD
extern USB_STATUS usb_class_serial_init_ipipe(CLASS_CALL_STRUCT_PTR);
extern USB_STATUS usb_class_serial_install_driver(CLASS_CALL_STRUCT_PTR, char *);
extern USB_STATUS usb_class_serial_uninstall_driver(CLASS_CALL_STRUCT_PTR);
#endif //MQX_USE_IO_OLD

#ifdef __cplusplus
}
#endif

#endif
